package com.xone.xapp;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class OverheadUserAdapter extends BasicListAdapter<OverheadListUserHolder> {
	
	protected ImageLoader imgLoader;
	protected Context context;
	protected LayoutInflater mInflater;
	public OverheadUserAdapter(Context context, List<JSONObject> list) {
		this.context = context;
		this.list = initWithJsonList(list);
		imgLoader = new ImageLoader(context);
		this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public List<OverheadListUserHolder> initWithJsonList(List<JSONObject> list) {
		if (JSONUtils.isNullOrEmptyList(list)) {
			return Collections.emptyList();
		}
		List<OverheadListUserHolder> result = new ArrayList<OverheadListUserHolder>();
		for (int i = 0; i < list.size(); i++) {
			result.add(new OverheadListUserHolder(list.get(i)));
		}
		return result;
	}
	
	/**
	 * 取得置顶的最后一个元素的位置
	 * @return
	 */
	public int getTopLastIndex() {
		return -1;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		ViewHolder viewHolder;
		if (view == null) {
			viewHolder = new ViewHolder();
			view = this.mInflater.inflate(R.layout.overhead_listview_user, null);
			viewHolder.setTitleView((TextView)view.findViewById(R.id.title));
			viewHolder.setDateCreatedView((TextView)view.findViewById(R.id.date_created));
			viewHolder.setDesc0View((TextView)view.findViewById(R.id.description0));
			viewHolder.setDesc1View((TextView)view.findViewById(R.id.description1));
			viewHolder.setIconView((ImageView)view.findViewById(R.id.icon));
			viewHolder.setStatusNameView((TextView)view.findViewById(R.id.statusName));
			view.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder)view.getTag();
		}
		OverheadListUserHolder holder = list.get(position);
		final String id = holder.getId();
		viewHolder.getTitleView().setText(holder.getTitleName());
		if ("0".equals(holder.getCheckStatus())) {
			viewHolder.getStatusNameView().setTextColor(context.getResources().getColor(R.color.check_status_color));
		} else if ("1".equals(holder.getCheckStatus())) {
			viewHolder.getStatusNameView().setTextColor(context.getResources().getColor(R.color.green));
		} else if ("2".equals(holder.getCheckStatus())) {
			viewHolder.getStatusNameView().setTextColor(context.getResources().getColor(R.color.red));
		}
		viewHolder.getStatusNameView().setText("状态：" + holder.getCheckStatusName());
		viewHolder.getDateCreatedView().setText(holder.getDateCreated());
		StringBuffer desc0 = new StringBuffer();
		desc0.append(",No.");
		desc0.append(holder.getId());
		desc0.append(",类型:");
		desc0.append(holder.getOverheadTypeName());
		viewHolder.getDesc0View().setText(desc0.toString());
		StringBuffer desc1 = new StringBuffer();
		desc1.append("产品编号:");
		desc1.append(holder.getRefId());
		String remark = holder.getRemark();
		if (StringUtils.isNotBlank(remark)) {
			desc1.append(",审核备注:");
			desc1.append(remark);
		}
		String desc = holder.getDesc();
		if (StringUtils.isNotBlank(desc)) {
			desc1.append(",描述:");
			desc1.append(desc);
		}
		viewHolder.getDesc1View().setText(desc1.toString());
		String imageId = holder.getImageId();
		if (StringUtils.isNotBlank(imageId)) {
			imgLoader.displayImage(Constants.APP_URL + "/assistant/image.html?id=" + imageId, viewHolder.getIconView());
		}
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(context, OverheadItemUserActivity.class);
				i.putExtra("id", id);
				context.startActivity(i);
			}
		});
		return view;
	}
	
	protected class ViewHolder {
		protected TextView titleView;
		protected TextView dateCreatedView;
		protected TextView statusNameView;
		protected TextView desc0View;
		protected TextView desc1View;
		protected ImageView iconView;
		public TextView getTitleView() {
			return titleView;
		}
		public void setTitleView(TextView titleView) {
			this.titleView = titleView;
		}
		public TextView getDateCreatedView() {
			return dateCreatedView;
		}
		public void setDateCreatedView(TextView dateCreatedView) {
			this.dateCreatedView = dateCreatedView;
		}
		public TextView getDesc0View() {
			return desc0View;
		}
		public void setDesc0View(TextView desc0View) {
			this.desc0View = desc0View;
		}
		public TextView getDesc1View() {
			return desc1View;
		}
		public void setDesc1View(TextView desc1View) {
			this.desc1View = desc1View;
		}
		public ImageView getIconView() {
			return iconView;
		}
		public void setIconView(ImageView iconView) {
			this.iconView = iconView;
		}
		public TextView getStatusNameView() {
			return statusNameView;
		}
		public void setStatusNameView(TextView statusNameView) {
			this.statusNameView = statusNameView;
		}
	}

}
